package com.online.teaching.module.student.homework.service.impl;

import com.online.teaching.dao.AnswerMapper;
import com.online.teaching.dao.HomeworkMapper;
import com.online.teaching.module.student.homework.domian.dto.AnswerAddDTO;
import com.online.teaching.module.student.homework.service.StudentHomeworkService;
import com.online.teaching.module.student.takes.domain.vo.GetHomeWorkListVO;
import com.online.teaching.pojo.Answer;
import com.online.teaching.pojo.Homework;
import com.online.teaching.utils.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : StudentBlogServiceImpl  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:21  //时间
 */
@Service
public class StudentHomeworkServiceImpl implements StudentHomeworkService {

    @Autowired
    private HomeworkMapper homeworkMapper;

    @Autowired
    private AnswerMapper answerMapper;


    @Override
    public List<GetHomeWorkListVO> getHomeworkList(Integer teachesId,Integer stuId) {
        //公用一下接口啦
        List<Homework> homework = homeworkMapper.teacherGetHomeWorkList(teachesId);
        List<GetHomeWorkListVO> getHomeWorkListVOS = SmartBeanUtil.copyList(homework, GetHomeWorkListVO.class);
        //非主键查询那个顺序莫名其妙老是乱，就for循环了，虽然性能不咋地
        for (int i = 0; i < getHomeWorkListVOS.size(); i++) {
            Answer studentAnswer = answerMapper.findStudentAnswer(stuId, getHomeWorkListVOS.get(i).getHomeworkId());
            if(studentAnswer!=null){
                getHomeWorkListVOS.get(i).setAnswerScore(studentAnswer.getAnswerScore());
                getHomeWorkListVOS.get(i).setAnswerContent(studentAnswer.getAnswerContent());
            }
        }
        return getHomeWorkListVOS;
    }

    @Override
    public int addAnswer(AnswerAddDTO answerAddDTO) {
        Answer answer = SmartBeanUtil.copy(answerAddDTO, Answer.class);
        if(answer.getAnswerId()!=null){
            return answerMapper.updateByPrimaryKeySelective(answer);
        }else {
            return answerMapper.insertSelective(answer);
        }
    }
}
